/*
    The I Ching (the Book of Changes) plasmoid.
    Copyright (C) 2002-2009 Jean Pierre Charalambos.
    TrigramSelectorGenerator Jorge Andrés Martínez

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
#include "trigramselectorgenerator.h"

//QT
#include <QGraphicsGridLayout>
#include <QGridLayout>
#include <QGraphicsLinearLayout>
#include <QLabel>
#include <QGraphicsWidget>
#include <QStackedWidget>

//Plasma
#include <Plasma/ComboBox>
#include <Plasma/GroupBox>
#include <Plasma/Label>

/**
* Clase que actualiza un hexagrama registrado en esta constructora por medio de la interacción del usuario con un TrigramSelector
* debe ser agregado en un QGraphicsWidget/layout ya que es un elemento grafico.
*
**/
TrigramSelectorGenerator::TrigramSelectorGenerator ( Hexagram* hex, QGraphicsWidget* parent ) : AbstractGenerator ( hex, parent ) {
    hexagram = hex;
    init();
    connect ( upperTrigramSelector, SIGNAL ( textChanged ( QString ) ), this, SLOT ( setHexagram ( QString ) ) );
    connect ( lowerTrigramSelector, SIGNAL ( textChanged ( QString ) ), this, SLOT ( setHexagram ( QString ) ) );
    setInterface();
    
}

/**
* Inicializa los elementos graficos del generator, el TrigramSelector de arriba, el de abajo y un Label con una imagen
* del hexagrama resultante.
*
**/
void TrigramSelectorGenerator::init() {
    gridGroup = new Plasma::GroupBox ( this );
    QStackedWidget *stackedWidget = new QStackedWidget();
    linearGroupLayout = new QGraphicsLinearLayout ( Qt::Vertical, gridGroup );
    
    upperTrigramSelector = new TrigramSelector(this);
    upperTrigramSelector->setMinimumSize(200, 200);
    linearGroupLayout->addItem ( upperTrigramSelector );
    upperTrigramSelector->setSizePolicy ( QSizePolicy::Minimum, QSizePolicy::Minimum );
    
    lowerTrigramSelector = new TrigramSelector(this);
    lowerTrigramSelector->setMinimumSize(200, 200);
    linearGroupLayout->addItem ( lowerTrigramSelector );
    lowerTrigramSelector->setSizePolicy ( QSizePolicy::Minimum, QSizePolicy::Minimum );
    
    
    linearGroupLayout->addItem ( upperTrigramSelector );

    hexagramDisplay = new Plasma::Label();
    hexagramDisplay->nativeWidget()->setAlignment ( Qt::AlignCenter );
    hexagramDisplay->nativeWidget()->setSizePolicy ( QSizePolicy::Preferred, QSizePolicy::Fixed );
    linearGroupLayout->addItem ( hexagramDisplay );
    
    //gridGroup->setLayout(linearGroupLayout);
    setWidget ( gridGroup );
    
}

/**
* Inicializa los elementos graficos que dependen de los slots setHexagram registrados en la constructora.
* uno para l trigrama superior y otro para el inferior.
*
**/
void TrigramSelectorGenerator::setInterface() {
    int hexCode = hexagram->getId();
    QString hexCodeStr;
    hexCodeStr.setNum ( hexCode );
    hexagramDisplay->setImage("/usr/lib/kde4/plasma_applet_iching/"+hexCodeStr+".jpg");
}

/**
* Slot que cambia el trigrama mostrado.
* @param id nombre del trigrama que se cambió.
**/
void TrigramSelectorGenerator::setHexagram ( QString id ) {

    int upperTrigram = nameToId ( upperTrigramSelector->getSelected() );
    int lowerTrigram = nameToId ( lowerTrigramSelector->getSelected() );

    Hexagram *newHexagram = new Hexagram ( new Trigram ( upperTrigram ),new Trigram ( lowerTrigram) );
    hexagram->setId ( newHexagram->getId() );
}

/**
* Metodo auxiliar que retorna el id de la clase Trigram dado el nombre del trigrama.
*
**/
int TrigramSelectorGenerator::nameToId ( QString name ) {
    if ( name == "ch'ien" ) return 1;
    if ( name == "chen" ) return 2;
    if ( name == "kan" ) return 3;
    if ( name == "ken" ) return 4;
    if ( name == "kun" ) return 5;
    if ( name == "sun" ) return 6;
    if ( name == "li" ) return 7;
    if ( name == "tui" ) return 8;
    return 1;
}
